package ex.hql.exception;

/**
 *@program:yeb
 *@author: hql
 *@Time: 2021/8/23  21:01
 *@description: 全局异常处理
 */

import ex.hql.pojo.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(SQLException.class)
    public RespBean mySqlException(SQLException e){
        if(e instanceof SQLIntegrityConstraintViolationException){
            return RespBean.error("该数据被其他地方关联,操作失败！");
        }
        return RespBean.error("数据库异常,操作失败！");
    }

}
